<template>
  <div>
    <div class="container">
      <div class="handle-box">
        <!-- <el-input v-model="search" placeholder="用户真实姓名" class="handle-input mr10" width="55"></el-input> -->
        <!-- <el-button type="primary" icon="el-icon-add" @click="showAdd = true">添加</el-button> -->
        <el-select class="handle-input" v-model="search.superMember" placeholder="用户等级" @change="getData()" clearable>
          <el-option v-for="item in superMemberList" :key="item.id" :label="item.memberName" :value="item.id"></el-option>
        </el-select>
      </div>
      <el-table :data="tableData" border class="table" ref="multipleTable" header-cell-class-name="table-header">
        <el-table-column prop="mobile" label="手机号"></el-table-column>
        <el-table-column prop="cityName" label="市级名称"></el-table-column>
        <el-table-column prop="districtName" label="区级名称"></el-table-column>
        <el-table-column prop="provincialName" label="省级名称"></el-table-column>
        <el-table-column prop="superMember" label="用户等级"></el-table-column>
        <el-table-column prop="upgradeTime" label="修改时间"></el-table-column>
        <el-table-column prop="updateTime" label="创建时间" :formatter="dateFormat"></el-table-column>
        <el-table-column label="操作" width="180" align="center">
          <template #default="scope">
            <el-button type="text" @click="handleEdit(scope.row)">设置代理</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex"
          :page-size="query.pageSize" :total="pageTotal" @current-change="handlePageChange"></el-pagination>
      </div>
    </div>
    <!-- 编辑弹出框 -->
    <el-dialog title="设置代理" v-model="showEdit" width="30%">
      <el-form ref="form" :model="from" label-width="70px">
        <el-form-item label="省级">
          <el-select class="handle-input" v-model="from.provincialCode" @change="changeProvincial" placeholder="请选择"
            clearable>
            <el-option v-for="item in provinceList" :key="item.AreaId" :label="item.AreaName"
              :value="item.AreaId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="市级">
          <el-select class="handle-input" v-model="from.cityCode" @change="changeCity" placeholder="请选择" clearable>
            <el-option v-for="item in cityList" :key="item.AreaId" :label="item.AreaName"
              :value="item.AreaId"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="区级">
          <el-select class="handle-input" v-model="from.districtCode" placeholder="请选择" clearable>
            <el-option v-for="item in areaList" :key="item.AreaId" :label="item.AreaName"
              :value="item.AreaId"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="showAdd = false">取 消</el-button>
          <el-button type="primary" @click="edit">确 定</el-button>
        </span>
      </template>
    </el-dialog>

  </div>
</template>

<script>
import { api, getApi } from "@/api/index";
import { formatDate } from "@/common/data";
import moment from "moment";
export default {
  inject: ["reload"],
  filters: {
    formatDate(time) {
      time = time * 1000;
      let date = new Date(time);
      console.log(new Date(time));
      return formatDate(date, "yyyy-MM-dd hh:mm");
    },
  },
  name: "basetable",
  data() {
    return {
      query: {
        pageNo: 1,
      },
      search: { superMember: null },
      tableData: [],
      // multipleSelection: [],
      pageTotal: 0,
      showEdit: false,
      from: {},

      superMemberList: [],
      provinceList: [],
      cityList: [],
      areaList: [],
    };
  },
  created() {
    this.getLevel()
  },
  mounted() { },
  methods: {
    dateFormat: function (row, column) {
      var date = row[column.property];
      if (date == undefined) {
        return "";
      }
      return moment(date).format("YYYY-MM-DD HH:mm:ss");
    },
    getLevel() {
      api('/admin/usersMemberConfig/selectAgentName', {
        parentId: this.$store.state.adminMsg.parentId,
      }).then((res) => {
        this.superMemberList = res.data
        this.search.superMember = res.data[0].id
        this.getData();
      });
    },
    // 获取分类列表
    getData() {
      api('/admin/subscribersAgent/select', {
        parentId: this.$store.state.adminMsg.parentId,
        superMember: this.search.superMember,
        limit: 10,
        offset: this.query.pageNo,
      }).then((res) => {
        // console.log(res.data);
        this.tableData = res.data.rows;
        this.pageTotal = res.data.total;
      });
    },
    // 触发搜索按钮
    handleSearch() {
      this.getData();
    },
    // 删除操作
    handleEdit(row) {
      this.showEdit = true;
      this.getProvince()
      this.from = JSON.parse(JSON.stringify(row));
    },
    getProvince() {
      getApi('/api/area/getProvince').then((res) => {
        this.provinceList = res.data
      });
    },
    getCity(provincialCode) {
      getApi(`/api/area/getCity?provincialCode=${provincialCode}`).then((res) => {
        this.cityList = res.data
      });
    },
    getArea(cityCode) {
      getApi(`/api/area/getArea?cityCode=${cityCode}`).then((res) => {
        this.areaList = res.data
      });
    },
    changeProvincial() {
      this.getCity(this.from.provincialCode)
    },
    changeCity() {
      this.getArea(this.from.cityCode)
    },
    edit() {
      api('/admin/subscribersAgent/setUp', this.from).then((res) => {
        if (res.code === 2000) {
          this.$message.success("修改商品成功");
          this.showEdit = false;
          this.list = {};
          this.getData();
        } else {
          this.$message.error(res.message);
        }
      });
    },
    // 分页导航
    handlePageChange(val) {
      // console.log(val);
      this.query.pageNo = val;
      this.getData();
    },
  },
};
</script>

<style lang='scss' scoped>
.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}

.table {
  width: 100%;
  font-size: 14px;
}

.red {
  color: #ff0000;
}

.mr10 {
  margin-right: 10px;
}

.table-td-thumb {
  display: block;
  margin: auto;
  width: 40px;
  height: 40px;
}

.crop-demo {
  display: flex;
  align-items: flex-end;
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  width: 178px;
  height: 178px;

  .pre-img {
    width: 178px;
    height: 178px;
    background: #f8f8f8;
    border: 1px solid #eee;
    border-radius: 5px;
    object-fit: cover;
  }

  .crop-input {
    position: absolute;
    width: 178px;
    height: 178px;
    left: 0px;
    top: 0px;
    opacity: 0;
    cursor: pointer;
  }

  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    line-height: 178px;
    text-align: center;
  }
}
</style>
